/*
 * @Author: lei 2677065112@qq.com
 * @Date: 2022-10-03 15:05:26
 * @LastEditors: lei 2677065112@qq.com
 * @LastEditTime: 2022-10-06 10:53:44
 * @FilePath: \vue-admin-template\src\router\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 静态路由表（规则对象数组）
export const constantRoutes = [
  {
    path: '/login',
    // 问题：
    // 如果都是在上面import一起引入这些页面组件，在webpack打包的时候，会导致首屏加载了一些不需要显示的组件，使首屏加载过慢
    // 解决：
    // webpack提供了import导入函数，component也可以使用函数形式定义导入过程
    // 会返回一个promise对象
    // 写法：component: () => import('组件文件路径')

    // 在webpack打包代码时，就会给这些组件分成多个js文件，而app.js里代码会减少
    component: () => import(/* webpackChunkName:"Login" */ '@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard', // 默认打开项目时路由地址为/,然后重定向为/dashboard
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

// 生成路由对象
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
